from py2neo import Graph, NodeMatcher


class Neo4jConfig(object):
    def __init__(self):
        print("Neo4j Connecting ...")
        self.graph = Graph("bolt://localhost:7687", username="neo4j", password="neo4j")
        self.match = NodeMatcher(self.graph)

    # 实体查询，用于命名实体识别：品牌+车系+车型
    def query_entity(self, value):
        answer = self.graph.run("MATCH (brand) WHERE brand.brand_name = \"" + value + "\" RETURN brand").data()
        return answer

    # 实体查询
    def get_entity_relation_by_entity(self, value):
        # 查询实体：不考虑实体类型，只考虑关系方向
        answer = self.graph.run(
            "MATCH (brand) - [] -> (entity)  WHERE brand.brand_name = \"" + value + "\" RETURN rel, entity").data()
        if len(answer) == 0:
            # 查询实体：不考虑关系方向
            answer = self.graph.run(
                "MATCH (brand) - [] - (entity)  WHERE brand.brand_name = \"" + value + " \" RETURN rel, entity").data()
        return answer

    def match_entity_item(self, value):
        pass
